package HomeElements;

import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObject2;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.util.Log;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import static util.GetDevice.getDevice;
import static HomeElements.Elements.*;

/**
 * Created by zhe.xiao on 2017/6/23.
 */

public class Action {
    public Action(){

    }
    private static UiDevice device = getDevice();

    /**
     *启动app
     */
    public static void startQunar(){
        try {
            device.executeShellCommand("am start -n com.Qunar/com.mqunar.splash.SplashActivity");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 退出app
     */
    public static void stopQunar(){
        try {
            device.executeShellCommand("am force-stop com.Qunar");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 等待首页加载
     */
    public static void waitShouYeElement(){
        UiObject2 ele = shouYeElement();
//        Log.v("UIAUTOMATOR", "shouye:" + ele.getText());
    }

    /**
     * 点击签证*WiFi按钮
     */
    public static void clickWifi(){
        UiObject2 ele = wifiElement();
        sleep();
        ele.click();
    }

    /**
     * 点击出境游按钮
     */
    public static void clickChuJingYou(){
        UiObject2 ele = chuJingYouElement();
        sleep();
        ele.click();
    }

    /**
     * 点击接送机按钮
     */
    public static void clickJieSongJI(){
        UiObject2 ele = jieJiSongElement();
        sleep();
        ele.click();
    }

    /**
     * 点击一日游按钮
     */
    public static void clickYiRiYou(){
        UiObject2 ele = yiRiYouElement();
        sleep();
        ele.click();
    }

    /**
     * 点击智慧旅行按钮
     */
    public static void clickZhiHuiLvXing(){
        UiObject2 ele = zhiHuiLvXingElement();
        sleep();
        ele.click();
    }

    /**
     * 点击定制游按钮
     */
    public static void clickDingZhiYou(){
        UiObject2 ele = dingZhiYouElement();
        sleep();
        ele.click();
    }

    /**
     * 点击特色住按钮
     */
    public static void clickTeSeZhu(){
        UiObject2 ele = teSeZhuElement();
        sleep();
        ele.click();
    }

    /**
     * 点击领券中心按钮
     */
    public static void clickLingQunaZhongXin(){
        UiObject2 ele = lingQuanZhongXinElement();
        sleep();
        ele.click();
    }

    /**
     * 点击踏青赏花按钮
     */
    public static void clickTaQingShangHua(){
        UiObject2 ele = taQingShangHuaElement();
        sleep();
        ele.click();
    }

    /**
     * 点击特价游按钮
     */
    public static void clickTeJiaYou(){
        UiObject2 ele = teJiaYouElement();
        sleep();
        ele.click();
    }

    /**
     * 点击大搜按钮
     */
    public static void clickDaShou(){
        UiObject2 ele = daSouElement();
        sleep();
        ele.click();
    }

    /**
     * 点击广告
     */
    public static void clickGuangGao(){
        UiObject2 ele= guangGaoElement();
        sleep();
        ele.click();
    }
    /**
     * 点击最近关注更多
     */
    public static void clickZuiJinGuanZhuMore(){
        UiObject2 ele = zuiJinGuanZhuMoreElement();
        sleep();
        ele.click();
    }

    /**
     * 滑动到最近关注第一个
     */
    public static UiObject2 swipeZuiJinGuanZhu1(){
        UiObject2 ele = zuiJinGuanZhu1Element();
        return ele;
    }


    /**
     * 点击最近关注第一个
     */
    public static void clickZuiJinGuanZhu1(){
            UiObject2 ele = swipeZuiJinGuanZhu1();
            sleep();
            ele.click();
    }

    /**
     * 滑动到最近关注第二个
     */
    public static UiObject2 swipeZuiJinGuanZhu2(){
        UiObject2 ele = zuiJinGuanZhu2Element();
        return ele;
    }

    /**
     * 点击最近关注第二个
     */
    public static void clickZuiJinGuanZhu2(){
            UiObject2 ele = swipeZuiJinGuanZhu2();
            sleep();
            ele.click();
    }

    /**
     * 点击您可能想去第一个标签
     */
    public static void clickNinKeNeng1(){
        try {
            ninKeNeng1Element().click();
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 点击您可能想去第二个标签
     */
    public static void clickNinKeNeng2(){
        try {
            ninKeNeng2Element().click();
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 点击您可能想去第三个标签
     */
    public static void clickNinKeNeng3(){
        try {
            ninKeNeng3Element().click();
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 您可能想去的全部标签
     */
    public static void clickNinKeNengMore(){
        UiObject2 ele = ninKeNengMoreElement();
        sleep();
        ele.click();
    }

    /**
     * 点击热门目的地第一个标签
     */
    public static void clickReMenMu1(){
        try {
            UiObject ele = reMenMu1Element();
            sleep();
            ele.click();
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 点击热门目的地第二个标签
     */
    public static void clickReMenMu2(){
        try {
            UiObject ele = reMenMu2Element();
            sleep();
            ele.click();
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 点击热门目的地第三个标签
     */
    public static void clickReMenMu3(){
        try {
            UiObject ele = reMenMu3Element();
            sleep();
            ele.click();
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 点击热门目的地更多
     */
    public static void clickReMenMuMore(){
        try {
            UiObject ele = reMenMuMoreElement();
            sleep();
            ele.click();
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 点击热门专题第一个标签
     */
    public static void clickReMenZhuan1(){
        UiObject2 ele = reMenZhuan1Element();
        sleep();
        ele.click();
    }

    /**
     * 点击热门专题第二个标签
     */
    public static void clickReMenZhuan2(){
        UiObject2 ele = reMenZhuan2Element();
        sleep();
        ele.click();
    }

    /**
     * 点击热门专题第三个标签
     */
    public static void clickReMenZhuan3(){
        UiObject2 ele = reMenZhuan3Element();
        sleep();
        ele.click();
    }

    /**
     * 点击热门专题更多
     */
    public static void clickReMenZhuanMore(){
        UiObject2 ele = reMenZhuanMoreElement();
        sleep();
        ele.click();
    }

    /**
     * 截图功能
     * @param fileName
     */
    public static void shotPic(String fileName){
        File dir = new File("/sdcard/xz");
        if(!dir.exists()){
            dir.mkdir();
        }
        File f = new File("/sdcard/xz/" + fileName + ".png");
        boolean d = device.takeScreenshot(f);
        Log.v("UIAUTOMATOR", "截图： " + d);
    }

    public static void back(){
        device.pressBack();

    }

    private static void sleep(){
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 最近关注：点击首页酒店按钮
     */
    public static void clickHomeHotel(){
        UiObject2 ele = homeHotelElement();
        sleep();
        ele.click();
    }

    /**
     * 最近关注： 点击酒店搜索按钮
     */
    public static void clickHotelSearch(){
        UiObject2 ele = hotelSearchElement();
        sleep();
        ele.click();
    }

    /**
     * 输入城市
     */
    public static void sendHotelName(){
        UiObject2 ele = sendHotelNameElement();
        List<String> l = new ArrayList<String>();
        l.add("北京");
        l.add("上海");
        l.add("广州");
        l.add("天津");
        Random random = new Random();
        int i = random.nextInt(4);
        ele.setText(l.get(i));
        device.pressEnter();
    }

    /**
     * 点击酒店城市
     */
    public static void clickHotelName(){
        UiObject2 ele = hotelNameElemenet();
        sleep();
        ele.click();
    }

    /**
     * 点击第一个酒店名字
     */
    public static void clickHotelName1(){
        UiObject2 ele = hotelName1Element();
        sleep();
        ele.click();
    }

    /**
     * 最近关注：点击酒店单品
     */
    public static void  clickHotel1Dan(){
        List<UiObject2> eleList = hotelDanElement();
        UiObject2 ele1 = eleList.get(2);
        sleep();
        ele1.click();
    }

    public static void  clickHotel2Dan(){
        List<UiObject2> eleList = hotelDanElement();
        UiObject2 ele1 = eleList.get(3);
        sleep();
        ele1.click();
    }

    public static void  clickHotel3Dan(){
        List<UiObject2> eleList = hotelDanElement();
        UiObject2 ele1 = eleList.get(4);
        sleep();
        ele1.click();
    }

    public static void  clickHotel4Dan(){
        List<UiObject2> eleList = hotelDanElement();
        UiObject2 ele1 = eleList.get(5);
        sleep();
        ele1.click();
    }


    /**
     * 设置输入法
     */
    public static void setShuRuFa(){
        device.openQuickSettings();
    }

    /**
     * 在大搜页面输入搜索信息
     */
    public static void daSouSendText(){
        List<String> l = new ArrayList<String>();
        l.add("北京");
        l.add("上海");
        l.add("广州");
        l.add("天津");
        Random random = new Random();
        int i = random.nextInt(4);
        daDouSearchElement().setText(l.get(i) + "的景点");
    }
    public static void clickTickSearch(){
        UiObject2 ele = tickElement();
        sleep();
        ele.click();
    }
    public static void clickTicketDan2(){
        UiObject2 ele = ticketDnaList().get(1);
        sleep();
        ele.click();
    }
    public static void clickTicketDan3(){
        UiObject2 ele = ticketDnaList().get(2);
        sleep();
        ele.click();
    }
    public static void clickTicketDan4(){
        UiObject2 ele = ticketDnaList().get(3);
        sleep();
        ele.click();
    }
    public static void clickTicketDan1(){
        UiObject2 ele = ticketDnaList().get(0);
        sleep();
        ele.click();
    }

    /**
     * 滑动到热门目的地
     */
    public static void swipeReMenMu(){
        reMenMuMoreElement();
    }

    /**
     * 滑动到广告6+1
     */
    public static void swipeGuangGao(){
        guangGao1Element();
    }

    /**
     * 广告6+1第一个点击
     */
    public static void clickGuangGao1(){
        UiObject2 ele = guangGao1Element();
        sleep();
        ele.click();
    }

    /**
     * 广告6+1第二个点击
     */
    public static void clickGuangGao2(){
        UiObject2 ele = guangGao2Element();
        sleep();
        ele.click();
    }

    /**
     * 广告6+1第三个点击
     */
    public static void clickGuangGao3(){
        UiObject2 ele = guangGao3Element();
        sleep();
        ele.click();
    }

    /**
     * 广告6+1第四个点击
     */
    public static void clickGuangGao4(){
        UiObject2 ele = guangGao4Element();
        sleep();
        ele.click();
    }

    /**
     * 广告6+1第五个点击
     */
    public static void clickGuangGao5(){
        UiObject2 ele = guangGao5Element();
        sleep();
        ele.click();
    }

    /**
     * 广告6+1第六个点击
     */
    public static void clickGuangGao6(){
        UiObject2 ele = guangGao6Element();
        sleep();
        ele.click();
    }

    /**
     * 广告6+1第七个点击
     */
    public static void clickGuangGao7(){
        UiObject2 ele = guangGao7Element();
        sleep();
        ele.click();
    }

    /**
     * 广告6+1第八个点击
     */
    public static void clickGuangGao8(){
        UiObject2 ele = guangGao8Element();
        sleep();
        ele.click();
    }

}
